package top.dingwen.io.treasure.api.upc;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Component;
import top.dingwen.io.treasure.api.base.constant.ApiConstant;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * LoadUserFactory : 加载用户工厂
 *
 * @author dingwen
 * @since 2025/3/4 14:14
 */
@Component
public class LoadUserFactory {
    /**
     * 加载用户
     */
    private static final Map<String, ILoadUser> LOAD_USER = new ConcurrentHashMap<>(8);

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        Map<String, ILoadUser> lus = SpringUtil.getBeansOfType(ILoadUser.class);
        lus.forEach((k, v) -> LOAD_USER.put(assembleComponentName(k), v));
    }

    /**
     * 组装组件名称
     *
     * @param method 方法
     * @return {@link String } 组件名称
     */
    private String assembleComponentName(String method) {
        return method.concat(ApiConstant.LOAD_USER_COMPONENT_PREFIX);
    }


    /**
     * 获取组件
     *
     * @param method 方式
     * @return {@link ILoadUser } 组件
     */
    public ILoadUser getLoadUser(String method) {
        return LOAD_USER.get(assembleComponentName(method));
    }

}
